<template>
  <el-dialog
    title="社团信息"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
    <el-form-item label="社团名称" prop="name">
      <el-input v-model="dataForm.name" placeholder="社团名称"></el-input>
    </el-form-item>
    <el-form-item label="社团资料" prop="introduce">
      <el-upload
        class="upload-demo"
        action="#"
        :on-preview="handlePreview"
        :on-remove="handleRemove"
        :before-remove="beforeRemove"
        :http-request="upload"
        multiple
        :limit="1"
        :on-exceed="handleExceed"
        :file-list="fileList">
        <el-button size="small" type="primary">点击上传</el-button>
      </el-upload>
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        fileList: [],
        options: [],
        dataForm: {
          id: 0,
          name: '',
          ownerId: '',
          ownerUser: '',
          status: '',
          academyId: '',
          academyName: '',
          introduce: '',
          createTime: '',
          acNum: '',
          budget: '',
          perNum: ''
        },
        dataRule: {
          name: [
            { required: true, message: '社团名称不能为空', trigger: 'blur' }
          ],
          ownerId: [
            { required: true, message: '负责人ID不能为空', trigger: 'blur' }
          ],
          ownerUser: [
            { required: true, message: '负责人姓名不能为空', trigger: 'blur' }
          ],
          status: [
            { required: true, message: '社团状态不能为空', trigger: 'blur' }
          ],
          academyId: [
            { required: true, message: '挂靠学院ID不能为空', trigger: 'blur' }
          ],
          academyName: [
            { required: true, message: '挂靠学院不能为空', trigger: 'blur' }
          ],
          introduce: [
            { required: true, message: '社团简介不能为空', trigger: 'blur' }
          ],
          createTime: [
            { required: true, message: '成立时间不能为空', trigger: 'blur' }
          ],
          acNum: [
            { required: true, message: '活动次数不能为空', trigger: 'blur' }
          ],
          budget: [
            { required: true, message: '经费不能为空', trigger: 'blur' }
          ],
          perNum: [
            { required: true, message: '成员人数不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      upload (param) {
        const formData = new FormData()
        formData.append('file', param.file)
        this.$http.post('http://localhost:8080/sys/upload', formData).then(resp => {
          console.log(resp)
          this.dataForm.introduce = resp.data.msg
        })
      },
      handleRemove (file, fileList) {
        console.log(file, fileList)
      },
      handlePreview (file) {
        console.log(file)
      },
      handleExceed (files, fileList) {
        this.$message.warning(`当前限制选择 1个文件，本次选择了 ${files.length} 个文件，共选择了 ${files.length + fileList.length} 个文件`)
      },
      beforeRemove (file, fileList) {
        return this.$confirm(`确定移除 ${file.name}？`)
      },
      init (id) {
        this.dataForm.id = id || 0
        this.visible = true
        this.$http({
          url: this.$http.adornUrl('/aca/academy/select'),
          method: 'get',
          params: this.$http.adornParams({
          })
        }).then(({data}) => {
          if (data && data.code === 0) {
            this.options = data.list
          } else {
          }
        })
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.id) {
            this.$http({
              url: this.$http.adornUrl(`/club/clubinfo/info/${this.dataForm.id}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.name = data.clubInfo.name
                this.dataForm.ownerId = data.clubInfo.ownerId
                this.dataForm.ownerUser = data.clubInfo.ownerUser
                this.dataForm.status = data.clubInfo.status
                this.dataForm.academyId = data.clubInfo.academyId
                this.dataForm.academyName = data.clubInfo.academyName
                this.dataForm.introduce = data.clubInfo.introduce
                this.dataForm.createTime = data.clubInfo.createTime
                this.dataForm.acNum = data.clubInfo.acNum
                this.dataForm.budget = data.clubInfo.budget
                this.dataForm.perNum = data.clubInfo.perNum
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/club/clubinfo/update`),
              method: 'post',
              data: this.$http.adornData({
                'id': this.dataForm.id || undefined,
                'name': this.dataForm.name,
                'ownerId': this.dataForm.ownerId,
                'ownerUser': this.dataForm.ownerUser,
                'status': this.dataForm.status,
                'academyId': this.dataForm.academyId,
                'academyName': this.dataForm.academyName,
                'introduce': this.dataForm.introduce,
                'createTime': this.dataForm.createTime,
                'acNum': this.dataForm.acNum,
                'budget': this.dataForm.budget,
                'perNum': this.dataForm.perNum
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
